#-*-coding:utf-8-*-
'''
Created on 2010-7-18

@author:zhaoyh603@163.com
'''
import web
from admin.config import view
class index():
    def GET(self):
        try:
            if web.ctx.session.logged_in == True:
                return '<h1>You are logged in</h1><a href="/logout">Logout</a>'
        except AttributeError:
            pass
        return view.login()

def authorize(func):
    def logged(*args,**dic):
        if web.ctx.session.logged_in==True:
            func(*args,**dic)
        else:
            raise web.seeother('/main')
    return logged
class login():
    def GET(self):
        try:
            web.ctx.session.logged_in = False
        except AttributeError:
            pass
        return view.login()
           
    def POST(self):
        login_data = web.input()
        if login_data.user=='':
            raise web.seeother('/')
        else:
            pwd=login_data.passwd
            m=auth.auth(pwd)            
            if m:
                web.ctx.session.logged_in = True
                web.ctx.session.user_id=login_data.user
                web.seeother('/admin/main')
            else:
                return "用户名或密码错误"
class logout():
    def GET(self):
        try:
            web.ctx.session.USER_ID = False
            web.ctx.session.kill()
        except AttributeError:
            pass
        raise web.seeother('/')
class changepwd():
    def POST(self):
        pwd_data=web.input()
        if pwd_data:
            try:
                cuser=user(id=web.ctx.session.user_id)
            except AttributeError:
                return '{"statusCode":"300", "message":"登陆超时，或没有登陆，请重新登陆!"}'
            if pwd_data.newPassword==pwd_data.rnewPassword:
                if cuser.changepwd(old=pwd_data.oldPassword,new=pwd_data.newPassword):
                    return '{"statusCode":"200", "message":"操作成功", "navTabId":"navNewsLi", "forwardUrl":"", "callbackType":"closeCurrent"}'
                else:
                    return '{"statusCode":"300", "message":"密码不正确"}'
            else:
                return '{"statusCode":"300", "message":"两次输入的密码不一致"}'

        else:
            return view.changepwd()
        
        
        
        
        
        
        